package cn.tedu._03vehicle.controller;

import cn.tedu._03vehicle.base.response.JsonResult;
import cn.tedu._03vehicle.base.response.StatusCode;
import cn.tedu._03vehicle.mapper.VehicleMapper;
import cn.tedu._03vehicle.pojo.dto.UserVo;
import cn.tedu._03vehicle.pojo.dto.VehicleAddParam;
import cn.tedu._03vehicle.pojo.dto.VehicleListQuery;
import cn.tedu._03vehicle.pojo.entity.Vehicle;
import cn.tedu._03vehicle.pojo.vo.VehicleListVO;
import com.github.xiaoymin.knife4j.annotations.ApiOperationSupport;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import springfox.documentation.annotations.ApiIgnore;

import javax.servlet.http.HttpSession;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

@Slf4j
@Api(tags = "02.车辆管理模块")
@RestController
@RequestMapping("/v1/vehicle")
public class VehicleController {

    @Autowired
    private VehicleMapper vehicleMapper;

    @PostMapping("add")
    @ApiOperation("添加车辆功能")
    @ApiOperationSupport(order = 10)
    public JsonResult addVehicle(@RequestBody VehicleAddParam vehicleAddParam, @ApiIgnore HttpSession session) {
        log.debug("vehicleAddParam " + vehicleAddParam);

        //校验用户状态
        UserVo userVo = (UserVo) session.getAttribute("user");
        log.debug("userVo = " + userVo);
        if (userVo == null) { //如果等于null
            return new JsonResult(StatusCode.NOT_LOGIN); //未登录
        }
        Vehicle vehicle = new Vehicle();
        BeanUtils.copyProperties(vehicleAddParam, vehicle);
        vehicle.setCreateTime(new Date());
        vehicle.setUpdateTime(new Date());
        vehicle.setUserId(userVo.getId());
        vehicleMapper.insertVehicle(vehicle);
        log.debug("vehicle " + vehicle);

        return JsonResult.ok();
    }

    @GetMapping("list")
    @ApiOperation("车辆列表功能")
    @ApiOperationSupport(order = 20)
    public JsonResult vehicleList(VehicleListQuery vehicleListQuery, @ApiIgnore HttpSession session) {
        log.debug("vehicleListQuery " + vehicleListQuery);
        UserVo userVo = (UserVo) session.getAttribute("user");

        if (userVo == null) {
            return new JsonResult(StatusCode.NOT_LOGIN);

        }
        String brand = vehicleListQuery.getBrand();
        String license = vehicleListQuery.getLicense();
        Long userId = userVo.getId();

        List<VehicleListVO> list = vehicleMapper.vehicleList(brand, license, userId);

        return JsonResult.ok(list);
    }

    @PostMapping("del")
    @ApiOperation("删除车辆功能")
    @ApiOperationSupport(order = 20)
    public JsonResult delVehicle(@PathVariable Long id, @ApiIgnore HttpSession session) {
        log.debug("id " + id);
        UserVo userVo = (UserVo) session.getAttribute("user");
        if (userVo == null) {
            return new JsonResult(StatusCode.NOT_LOGIN);
        } else {
            Long userId = userVo.getId();
            int i = vehicleMapper.delVehicle(id, userId);
            if (i > 0) {
                return JsonResult.ok(id);
            } else {
                return new JsonResult(StatusCode.NOT_ALLOWED);
            }
        }
    }

}


